package com.yehui.algorithm.sword;

/**
 * 连续子数组的最大和
 * Created by XuChunH on 2016/9/16.
 */
public class FindGreatestSumOfSubArray {
    public int FindGreatestSumOfSubArray(int[] array) {
        if(array == null || array.length == 0){
            return 0;
        }
        int maxSum = array[0];
        int currentSum = 0;
        for (int i = 0; i < array.length; i++) {
            currentSum += array[i];
            maxSum = maxSum > currentSum ? maxSum : currentSum;
            if(currentSum < 0) {
                currentSum = 0;
            }
        }
        return maxSum;
    }
}
